import 'package:flutter/material.dart';
import 'package:Second/routerdemo/TabsLogin.dart' as otherTabsLogin;
import 'package:Second/routerdemo/user/Register.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  final Map<String, Function> routes = {
    "/": (context) => TabsLogin(),
    "/otherTabs": (context) => otherTabsLogin.TabsLogin(),
    "/register": (context) => RegisterPage(),
  };
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // home: TabsLogin(),
      initialRoute: '/',
      onGenerateRoute: (RouteSettings settings) {
        final String? name = settings.name;
        final Function? pageContentBuilder = this.routes[name];

        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
      },
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class TabsLogin extends StatefulWidget {
  TabsLogin({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<TabsLogin> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Drawer'),
      ),
      body: Text("abc"),
      // 左侧抽屉
      drawer: Drawer(
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  //还有个DrawerHeader组件，详情看右边抽屉实现，代码见下面，但UserAccountsDrawerHeader更适合名片式的
                  child: UserAccountsDrawerHeader(
                    accountName: Text("用户"),
                    accountEmail: Text("dyp@163.com"),
                    currentAccountPicture: CircleAvatar(
                      backgroundImage: NetworkImage(
                          "https://pic3.zhimg.com/v2-d109b212df1b784ccc65ff905524134a_1440w.jpg?source=172ae18b"),
                    ),
                  ),
                )
              ],
            ),
            ListTile(
                title: Text("My Space"),
                leading: CircleAvatar(
                  child: Icon(Icons.home),
                ),
                //listTile的点击事件响应
                onTap: () {
                  //弹出侧边栏
                  Navigator.of(context).pop();
                }),
            Divider(),
            ListTile(
              title: Text("My Setting"),
              leading: CircleAvatar(
                child: Icon(Icons.settings),
              ),
              onTap: () {
                Navigator.pushNamed(context, "/otherTabs");
              },
            )
          ],
        ),
      ),
      //右侧抽屉
      endDrawer: Drawer(
          child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: DrawerHeader(
                    child: Text('nihao'),
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage(
                                "https://pic3.zhimg.com/v2-d109b212df1b784ccc65ff905524134a_1440w.jpg?source=172ae18b",
                                scale: 1.0),
                            fit: BoxFit.cover))),
              )
            ],
          ),
          ListTile(
            title: Text("My Space"),
            leading: CircleAvatar(
              child: Icon(Icons.home),
            ),
          ),
          Divider(),
          ListTile(
            title: Text("My Setting"),
            leading: CircleAvatar(
              child: Icon(Icons.settings),
            ),
          )
        ],
      )),
    );
  }
}
